package com.tepth.appmanageservice.tools.ext;

import android.os.Environment;
import android.support.annotation.NonNull;

import com.tepth.appmanageservice.tools.Ref;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

public class ExtIO {
    private static String _sdPath;

    public static String getFileDir(String dir) {
        return getSdPath() + dir;
    }

    public static String getConfigFileText() {
        File file = new File(getConfigFileName());
        FileInputStream stream;

        try {
            if (file.exists()) {
                stream = new FileInputStream(file);

                byte[] data = new byte[stream.available()];
                stream.read(data);
                stream.close();

                return new String(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void saveConfigFile(String string) {
        File file = new File(getConfigFileName());
        FileOutputStream fos;

        try {
            file.createNewFile();
            fos = new FileOutputStream(file);
            fos.write(string.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static File createSdDir(String dirName) {
        File dir = new File(getSdPath() + dirName);
        dir.mkdir();
        return dir;
    }

    @NonNull
    public static String getMd5ByFile(File file) throws FileNotFoundException {
        String value = null;
        FileInputStream in = new FileInputStream(file);

        try {
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value.toUpperCase();
    }

    public static String getMd5ByFile(File file, Ref<Integer> size) throws FileNotFoundException {
        String value = null;
        FileInputStream in = new FileInputStream(file);

        try {
            size.set(in.available());
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String val = value.toUpperCase();
        if (val.length() == 31)
            val = "0" + val;
        if (val.length() == 30)
            val = "00" + val;
        return val;
    }

    private static String getSdPath() {
        if (_sdPath == null)
            _sdPath = Environment.getExternalStorageDirectory() + "/";
        return _sdPath;
    }

    private static String getRootDir() {
        return getSdPath();
    }

    private static String getConfigFileName() {
        return getRootDir() + "ai/appManage/Config.ini";
    }
}